;
(function($) {
	"use strict";

	//tab切换
	ECar.tab({
		selector: "#tabs",
		event: "click",
		delay: 200,
		selected: '.active'
	});
	ECar.tab({
		selector: "#pro-tab",
		event: "click",
		delay: 200,
		selected: '.active'
	})

	//商品数量操作
	$(".btn_number").on("click", function(e) {
		var oTarget = e.target || e.srcElement,
			sTagName = oTarget.nodeName.toLowerCase(),
			nFlag = 1,
			sCount = "",
			sType = "";
		if (sTagName !== "a") {
			return;
		}

		sType = $(oTarget).data("type");
		sCount = $("#good_number").val();
		nFlag = validNumber(sCount);
		if (nFlag === 0) {
			sCount = sCount.replace("0", "");
		} else if (nFlag === -1) {
			$("#good_number").val("1");
			return;
		}

		sCount = parseInt(sCount);
		switch (sType) {
			case "reduce":
				if (sCount > 1) {
					sCount = sCount - 1;
				}
				break;
			case "add":
				if (sCount < 99) {
					sCount = sCount + 1;
				}
		}
		$("#good_number").val(sCount);
	});

	//商品数量输入控制
	$("#good_number").on("keyup", function(e) {
		var sKeyCode = e.which || e.keyCode,
			sCount = $(this).val(),
			nFlag = validNumber(sCount);

		if ((sKeyCode >= 96 && sKeyCode <= 105) || (sKeyCode >= 48 && sKeyCode <= 57)) {
			return;
		}
		sCount = sCount.slice(0, 1);
		$(this).val(sCount);
	});
	$("#good_number").on("change", function(e) {
		var sCount = $(this).val(),
			nFlag = validNumber(sCount);

		switch (nFlag) {
			case 0:
				sCount = sCount.replace("0", "");
				break;
			case -1:
				sCount = "1";
				break;
			default:
				return;
		}

		$(this).val(sCount);
	});

	$("#good_price_tip").hover(function(e){
		$(this).prevAll("span").css("opacity", "1");
	},function(e){
		$(this).prevAll("span").css("opacity", "0");
	})

	$(".qr_code > span").hover(function(e){
		$(this).addClass("hover");
	}, function(e){
		$(this).removeClass("hover");
	})

	/**
	 * [校验商品数量格式是否正确]
	 */
	function validNumber(sCount) {
		if (/^[1-9][0-9]?$/.test(sCount)) {
			return 1;
		} else if (/^0[1-9]$/.test(sCount)) {
			return 0;
		} else {
			return -1;
		}
	}
})(jQuery);